﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.hud {
    import flash.events.*;
    import flash.utils.*;

    public class HUDCounter {

        protected static const TIMER_TICK:int = 50;
        protected static const COUNT_RATE:int = 30;

        protected var m_targetValue:Number = 0;
        protected var m_currentValue:Number = 0;
        protected var m_changeRate:Number = 0;
        protected var m_startTimer:Boolean = false;
        protected var m_updateTimer:Timer;
        protected var m_updateCallback:Function;

        public function HUDCounter(_arg1:Function){
            this.m_updateTimer = new Timer(TIMER_TICK);
            super();
            this.m_updateCallback = _arg1;
            this.m_updateTimer.addEventListener(TimerEvent.TIMER, this.updateCurrentValue);
        }
        public function get value():Number{
            return (this.m_targetValue);
        }
        public function set value(_arg1:Number):void{
            if (_arg1 == this.m_targetValue){
                return;
            };
            this.m_targetValue = _arg1;
            this.m_startTimer = true;
        }
        public function update(_arg1:Boolean=false):void{
            if (_arg1){
                this.m_currentValue = this.m_targetValue;
                this.m_updateCallback(this.m_currentValue);
            } else {
                if (this.m_startTimer){
                    this.m_changeRate = this.calculateChangeRate(this.m_currentValue, this.m_targetValue);
                    this.m_updateTimer.start();
                };
            };
            this.m_startTimer = false;
        }
        protected function updateCurrentValue(_arg1:TimerEvent):void{
            this.m_currentValue = (this.m_currentValue + this.m_changeRate);
            if ((((((this.m_changeRate > 0)) && ((this.m_currentValue > this.m_targetValue)))) || ((((this.m_changeRate < 0)) && ((this.m_currentValue < this.m_targetValue)))))){
                this.m_currentValue = this.m_targetValue;
            };
            this.m_updateCallback(this.m_currentValue);
            if (this.m_currentValue == this.m_targetValue){
                this.m_updateTimer.reset();
                this.m_updateTimer.stop();
            };
        }
        protected function calculateChangeRate(_arg1:int, _arg2:int):int{
            var _local3:int;
            if (_arg2 > _arg1){
                _local3 = Math.ceil(((_arg2 - _arg1) / COUNT_RATE));
            } else {
                if (_arg2 < _arg1){
                    _local3 = Math.floor(((_arg2 - _arg1) / COUNT_RATE));
                };
            };
            return (_local3);
        }

    }
}//package Display.hud 
